home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / dos / open.c < prev    next >
C/C++ Source or Header  |  1996-09-13  |  4KB  |  154 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: open.c,v 1.6 1996/09/13 17:50:07 digulla Exp $
  4.     $Log: open.c,v $
  5.     Revision 1.6  1996/09/13 17:50:07  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.5  1996/09/11 13:02:22  digulla
  9.     Open() and Lock() are two different functions now (M. Fleischer)
  10.  
  11.     Revision 1.4  1996/08/13 13:52:49  digulla
  12.     Replaced <dos/dosextens.h> by "dos_intern.h" or added "dos_intern.h"
  13.     Replaced __AROS_LA by __AROS_LHA
  14.  
  15.     Revision 1.3  1996/08/12 14:20:38  digulla
  16.     Added aliases
  17.  
  18.     Revision 1.2  1996/08/01 17:40:55  digulla
  19.     Added standard header for all files
  20.  
  21.     Desc:
  22.     Lang: english
  23. */
  24. #include <exec/memory.h>
  25. #include <clib/exec_protos.h>
  26. #include <utility/tagitem.h>
  27. #include <dos/dosextens.h>
  28. #include <dos/filesystem.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/utility_protos.h>
  31. #include "dos_intern.h"
  32.  
  33. #define NEWLIST(l)                          \
  34. ((l)->lh_Head=(struct Node *)&(l)->lh_Tail, \
  35.  (l)->lh_Tail=NULL,                         \
  36.  (l)->lh_TailPred=(struct Node *)(l))
  37.  
  38. /*****************************************************************************
  39.  
  40.     NAME */
  41.     #include <clib/dos_protos.h>
  42.  
  43.     __AROS_LH2(BPTR, Open,
  44.  
  45. /*  SYNOPSIS */
  46.     __AROS_LHA(STRPTR, name,       D1),
  47.     __AROS_LHA(LONG,   accessMode, D2),
  48.  
  49. /*  LOCATION */
  50.     struct DosLibrary *, DOSBase, 5, Dos)
  51.  
  52. /*  FUNCTION
  53.     Opens a file for read and/or write depending on the accessmode given.
  54.  
  55.     INPUTS
  56.     name       - NUL terminated name of the file.
  57.     accessMode - One of MODE_OLDFILE   - open existing file
  58.                 MODE_NEWFILE   - delete old, create new file
  59.                          exclusive lock
  60.                 MODE_READWRITE - open new one if it doesn't exist
  61.  
  62.     RESULT
  63.     Handle to the file or 0 if the file couldn't be opened.
  64.     IoErr() gives additional information in that case.
  65.  
  66.     NOTES
  67.  
  68.     EXAMPLE
  69.  
  70.     BUGS
  71.  
  72.     SEE ALSO
  73.  
  74.     INTERNALS
  75.  
  76.     HISTORY
  77.     29-10-95    digulla automatically created from
  78.                 dos_lib.fd and clib/dos_protos.h
  79.  
  80. *****************************************************************************/
  81. {
  82.     __AROS_FUNC_INIT
  83.     __AROS_BASE_EXT_DECL(struct DosLibrary *,DOSBase)
  84.  
  85.     struct FileHandle *ret;
  86.     BPTR con, ast;
  87.  
  88.     /* Get pointer to process structure */
  89.     struct Process *me=(struct Process *)FindTask(NULL);
  90.  
  91.     /* Create filehandle */
  92.     ret=(struct FileHandle *)AllocDosObject(DOS_FILEHANDLE,NULL);
  93.     if(ret!=NULL)
  94.     {
  95.     /* Get pointer to I/O request. Use stackspace for now. */
  96.     struct IOFileSys io,*iofs=&io;
  97.  
  98.     /* Prepare I/O request. */
  99.     iofs->IOFS.io_Message.mn_Node.ln_Type=NT_REPLYMSG;
  100.     iofs->IOFS.io_Message.mn_ReplyPort   =&me->pr_MsgPort;
  101.     iofs->IOFS.io_Message.mn_Length      =sizeof(struct IOFileSys);
  102.     iofs->IOFS.io_Flags=0;
  103.     iofs->IOFS.io_Command=FSA_OPEN_FILE;
  104.  
  105.     /* io_Args[0] is the name which is set by DoName(). */
  106.     switch(accessMode)
  107.     {
  108.         case MODE_OLDFILE:
  109.         iofs->io_Args[1]=FMF_WRITE|FMF_READ;
  110.         ast=con=me->pr_CIS;
  111.         break;
  112.         case MODE_NEWFILE:
  113.         iofs->io_Args[1]=FMF_LOCK|FMF_CREATE|FMF_CLEAR|FMF_WRITE|FMF_READ;
  114.         con=me->pr_COS;
  115.         ast=me->pr_CES?me->pr_CES:me->pr_COS;
  116.         break;
  117.         case MODE_READWRITE:
  118.         iofs->io_Args[1]=FMF_CREATE|FMF_WRITE|FMF_READ;
  119.         con=me->pr_COS;
  120.         ast=me->pr_CES?me->pr_CES:me->pr_COS;
  121.         break;
  122.         default:
  123.         iofs->io_Args[1]=accessMode;
  124.         ast=con=me->pr_CIS;
  125.         break;
  126.     }
  127.     iofs->io_Args[2]=FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE;
  128.     if(!Stricmp(name,"CONSOLE:"))
  129.     {
  130.         iofs->IOFS.io_Device=((struct FileHandle *)MKBADDR(con))->fh_Device;
  131.         iofs->IOFS.io_Unit    =((struct FileHandle *)MKBADDR(con))->fh_Unit;
  132.         iofs->io_Args[0]=(IPTR)"";
  133.         (void)DoIO(&iofs->IOFS);
  134.     }else if(!Stricmp(name,"*"))
  135.     {
  136.         iofs->IOFS.io_Device=((struct FileHandle *)MKBADDR(ast))->fh_Device;
  137.         iofs->IOFS.io_Unit    =((struct FileHandle *)MKBADDR(ast))->fh_Unit;
  138.         iofs->io_Args[0]=(IPTR)"";
  139.         (void)DoIO(&iofs->IOFS);
  140.     }else
  141.         (void)DoName(iofs,name);
  142.     if(!(me->pr_Result2=iofs->io_DosError))
  143.     {
  144.         ret->fh_Device=iofs->IOFS.io_Device;
  145.         ret->fh_Unit  =iofs->IOFS.io_Unit;
  146.         return MKBADDR(ret);
  147.     }
  148.     FreeDosObject(DOS_FILEHANDLE,ret);
  149.     }else
  150.     me->pr_Result2=ERROR_NO_FREE_STORE;
  151.     return 0;
  152.     __AROS_FUNC_EXIT
  153. } /* Open */
  154.